package com.mc2014.follow_me_2014;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;

public class SubmitActivity extends SherlockActivity {
	// camera
	private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
	private Uri fileUri;
	public static final int MEDIA_TYPE_IMAGE = 1;
	public static final int MEDIA_TYPE_VIDEO = 2;

	// actionbar
	private ActionBar actionbar;
	private View custom_actionbar_layout;
	private String nickName;
	private LayoutInflater inflater;
	private TextView actionbar_title;
	private LinearLayout actionbar_menu;
	private Button menu1, menu2, menu3;
	private View view_for_click;
	private Intent i;
	private ImageView triangle_img;
	private boolean isVisible = false;

	// ui
	private EditText topic_ed;
	private Button camera_btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_submit);

		// actionbar 셜록
		actionbar = getSupportActionBar();
		actionbar.setDisplayShowTitleEnabled(false);
		actionbar.setDisplayShowHomeEnabled(false);

		inflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		custom_actionbar_layout = inflater.inflate(R.layout.actionbar_layout,
				null);
		actionbar_title = (TextView) custom_actionbar_layout
				.findViewById(R.id.actionbar_title);
		actionbar_menu = (LinearLayout) findViewById(R.id.actionbar_menu);
		view_for_click = (View) findViewById(R.id.view_for_click);

		menu1 = (Button) findViewById(R.id.topic);
		menu2 = (Button) findViewById(R.id.fun_list);
		menu3 = (Button) findViewById(R.id.photo_list);

		camera_btn = (Button) findViewById(R.id.img_iv);

		triangle_img = (ImageView) custom_actionbar_layout
				.findViewById(R.id.triangle);

		nickName = "주제 등록하기";

		// 액션바 메뉴 설정
		actionbar_title.setText(nickName);
		actionbar_title.setOnClickListener(actionbarClick);
		view_for_click.setOnClickListener(viewClick);

		menu1.setOnClickListener(menuClick);
		menu2.setOnClickListener(menuClick);
		menu3.setOnClickListener(menuClick);

		camera_btn.setOnClickListener(cameraClick);

		actionbar.setCustomView(custom_actionbar_layout,
				new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
						ActionBar.LayoutParams.MATCH_PARENT));
		actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

		// ui for this activity

		topic_ed = (EditText) findViewById(R.id.topic_ed);

		
	}
	
	private OnClickListener actionbarClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (isVisible == false) {
				actionbar_menu.setVisibility(View.VISIBLE);
				view_for_click.setVisibility(View.VISIBLE);
				triangle_img.setImageResource(R.drawable.triangle_ch);
				isVisible = true;
			} else {
				actionbar_menu.setVisibility(View.GONE);
				view_for_click.setVisibility(View.GONE);
				triangle_img.setImageResource(R.drawable.triangle);
				isVisible = false;
			}
		}
	};

	private OnClickListener viewClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			actionbar_menu.setVisibility(View.GONE);
			view_for_click.setVisibility(View.GONE);
			triangle_img.setImageResource(R.drawable.triangle);

		}
	};

	private OnClickListener menuClick = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.topic:

				Intent i = new Intent(SubmitActivity.this, SubmitActivity.class);
				startActivity(i);
				finish();
				break;
			case R.id.fun_list:
				i = new Intent(SubmitActivity.this, ListLikedActivity.class);
				startActivity(i);
				finish();
				break;
			case R.id.photo_list:

				i = new Intent(SubmitActivity.this, ListUploadedActivity.class);
				startActivity(i);
				finish();
				break;
			}

		}
	};
	private OnClickListener cameraClick = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

			fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file
																// to save the
																// image
			intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image
																// file name

			// start the image capture Intent
			startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
			
		}
	};

	private static Uri getOutputMediaFileUri(int type) {
		return Uri.fromFile(getOutputMediaFile(type));
	}

	private static File getOutputMediaFile(int type) {
		// To be safe, you should check that the SDCard is mounted
		// using Environment.getExternalStorageState() before doing this.

		File mediaStorageDir = new File(
				Environment
						.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
				"MyCameraApp");
		// This location works best if you want the created images to be shared
		// between applications and persist after your app has been uninstalled.

		// Create the storage directory if it does not exist
		if (!mediaStorageDir.exists()) {
			if (!mediaStorageDir.mkdirs()) {
				Log.d("MyCameraApp", "failed to create directory");
				return null;
			}
		}

		// Create a media file name
		String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
				.format(new Date());
		File mediaFile;
		if (type == MEDIA_TYPE_IMAGE) {
			mediaFile = new File(mediaStorageDir.getPath() + File.separator
					+ "IMG_" + timeStamp + ".jpg");
		} else if (type == MEDIA_TYPE_VIDEO) {
			mediaFile = new File(mediaStorageDir.getPath() + File.separator
					+ "VID_" + timeStamp + ".mp4");
		} else {
			return null;
		}

		return mediaFile;
	}
}
